package com.strange.mdl.module.mdl.service.sutras;

import cn.hutool.core.collection.CollUtil;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.sutras.MdlSutrasPageReqVO;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.sutras.MdlSutrasSaveReqVO;
import com.strange.mdl.module.mdl.controller.app.sutras.vo.AppMdlSutrasPageReqVO;
import com.strange.mdl.module.mdl.dal.dataobject.sutras.MdlSutraVolumesDO;
import com.strange.mdl.module.mdl.dal.mysql.sutras.MdlSutraVolumesMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.strange.mdl.module.mdl.dal.dataobject.sutras.MdlSutrasDO;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;

import com.strange.mdl.module.mdl.dal.mysql.sutras.MdlSutrasMapper;

import static com.strange.mdl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.strange.mdl.framework.common.util.collection.CollectionUtils.convertList;
import static com.strange.mdl.module.mdl.enums.ErrorCodeConstants.*;

/**
 * 经书 Service 实现类
 *
 * @author 密谛灵总控
 */
@Service
@Validated
public class MdlSutrasServiceImpl implements MdlSutrasService {

    @Resource
    private MdlSutrasMapper sutrasMapper;

    @Resource
    private MdlSutraVolumesMapper sutraVolumesMapper;

    @Override
    @Transactional
    public Long createSutras(MdlSutrasSaveReqVO createReqVO) {
        // 插入经书
        MdlSutrasDO sutras = BeanUtils.toBean(createReqVO, MdlSutrasDO.class);
        sutrasMapper.insert(sutras);

        // 默认创建第一卷
        MdlSutraVolumesDO sutraVolumesDO = new MdlSutraVolumesDO();
        sutraVolumesDO.setSutraId(sutras.getId());
        sutraVolumesDO.setVolumeNumber(1);
        sutraVolumesDO.setVolumeTitle("第一卷：默认");
        sutraVolumesMapper.insert(sutraVolumesDO);

        // 返回
        return sutras.getId();
    }

    @Override
    public void updateSutras(MdlSutrasSaveReqVO updateReqVO) {
        // 校验存在
        validateSutrasExists(updateReqVO.getId());
        // 更新
        MdlSutrasDO updateObj = BeanUtils.toBean(updateReqVO, MdlSutrasDO.class);
        sutrasMapper.updateById(updateObj);
    }

    @Override
    public void deleteSutras(Long id) {
        // 校验存在
        validateSutrasExists(id);
        // 删除
        sutrasMapper.deleteById(id);
    }

    @Override
        public void deleteSutrasListByIds(List<Long> ids) {
        // 校验存在
        validateSutrasExists(ids);
        // 删除
        sutrasMapper.deleteByIds(ids);
        }

    private void validateSutrasExists(List<Long> ids) {
        List<MdlSutrasDO> list = sutrasMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(SUTRAS_NOT_EXISTS);
        }
    }

    private void validateSutrasExists(Long id) {
        if (sutrasMapper.selectById(id) == null) {
            throw exception(SUTRAS_NOT_EXISTS);
        }
    }

    @Override
    public MdlSutrasDO getSutras(Long id) {
        return sutrasMapper.selectById(id);
    }

    @Override
    public PageResult<MdlSutrasDO> getSutrasPage(MdlSutrasPageReqVO pageReqVO) {
        return sutrasMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<MdlSutrasDO> getSutrasPage(AppMdlSutrasPageReqVO pageReqVO) {
        return sutrasMapper.selectPage(pageReqVO);
    }

}